/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-24     86188       the first version
 */
#include "head_rtthread.h"
#include "spi_drv.h"
#include "drv_spi.h"

SPI_HandleTypedef SPI_Handle;

//#define ICM42688_SPI_DEVICE_NAME    "spi10"
//#define MS5611_SPI_DEVICE_NAME      "spi11"
//#define ICM20602_SPI_DEVICE_NAME    "spi12"
#define FM25V05_SPI_DEVICE_NAME     "spi20"

//#define ICM42688_SPI_DEVICE_CS    4  //PA4
//#define MS5611_SPI_DEVICE_CS      40 //PC8
//#define ICM20602_SPI_DEVICE_CS    46 //PC14
#define FM25V05_SPI_DEVICE_CS     27 //PB11


void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
    rt_sem_release(((struct stm32_spi *)hspi)->spi_bus.owner->user_data);
}
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
    rt_sem_release(((struct stm32_spi *)hspi)->spi_bus.owner->user_data);
}
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
    rt_sem_release(((struct stm32_spi *)hspi)->spi_bus.owner->user_data);
}
static int spi_device_init(void)
{
    /*-------------------------------------------------------------------*
     * attach device cs pin
     *-------------------------------------------------------------------*/
    /* cs_pin clock enable */
    __GPIOA_CLK_ENABLE();
    __GPIOB_CLK_ENABLE();
    __GPIOC_CLK_ENABLE();
    /* attach cs pin */
    //rt_hw_spi_device_attach("spi1", ICM42688_SPI_DEVICE_NAME,     GPIOA, GPIO_PIN_4);
    //rt_hw_spi_device_attach("spi1", MS5611_SPI_DEVICE_NAME,       GPIOC, GPIO_PIN_8);
    rt_hw_spi_device_attach("spi2", FM25V05_SPI_DEVICE_NAME,      GPIOB, GPIO_PIN_11);

    /*-------------------------------------------------------------------*
     * init specific spi device
     *-------------------------------------------------------------------*/
    struct rt_spi_configuration spi_cfg;

//    /* SPI1_device*/
//    SPI_Handle.SPI1dev.ICM42688 = (struct rt_spi_device *)rt_device_find(ICM42688_SPI_DEVICE_NAME);
//    if (SPI_Handle.SPI1dev.ICM42688 == RT_NULL)
//    {
//        rt_kprintf("failed to create spi1_device:ICM42688!\r\n");
//    }
//    SPI_Handle.SPI1dev.MS5611   = (struct rt_spi_device *)rt_device_find(MS5611_SPI_DEVICE_NAME);
//    if (SPI_Handle.SPI1dev.MS5611 == RT_NULL)
//    {
//        rt_kprintf("failed to create spi1_device:MS5611!\r\n");
//    }
    /* SPI2_device*/
    SPI_Handle.SPI2dev.FM25V05   = (struct rt_spi_device *)rt_device_find(FM25V05_SPI_DEVICE_NAME);
    if (SPI_Handle.SPI2dev.FM25V05 == RT_NULL)
    {
        rt_kprintf("failed to create spi2_device:FM25V05!\r\n");
    }

    spi_cfg.data_width = 8;
    spi_cfg.mode       = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
    spi_cfg.max_hz     = 21000000;  //10M
//    rt_spi_configure(SPI_Handle.SPI1dev.ICM42688,  &spi_cfg);
//    rt_spi_configure(SPI_Handle.SPI1dev.MS5611,    &spi_cfg);
    rt_spi_configure(SPI_Handle.SPI2dev.FM25V05,   &spi_cfg);

    /* semaphore be used to relax CPU when SPI is working */
//    SPI_Handle.SPI1dev.sem = rt_sem_create("spidma_sem", 0, RT_IPC_FLAG_PRIO);
//    if (SPI_Handle.SPI1dev.sem == RT_NULL)
//    {
//        rt_kprintf("failed to create spidma_sem!\r\n");
//    }
    SPI_Handle.SPI2dev.sem = rt_sem_create("spi2dma_sem", 0, RT_IPC_FLAG_PRIO);
    if (SPI_Handle.SPI2dev.sem == RT_NULL)
    {
        rt_kprintf("failed to create spi2dma_sem!\r\n");
    }

    /* 同一总线,公用一个信号量 */
    /* SPI1_bus semaphore*/
    //SPI_Handle.SPI1dev.ICM42688->user_data = (void *)SPI_Handle.SPI1dev.sem;
    //SPI_Handle.SPI1dev.MS5611->user_data   = (void *)SPI_Handle.SPI1dev.sem;
    /* SPI2_bus semaphore*/
    SPI_Handle.SPI2dev.FM25V05->user_data  = (void *)SPI_Handle.SPI2dev.sem;


    SPI_Handle.RWBytes = rt_spi_transfer;
    SPI_Handle.WthenWs = rt_spi_send_then_send;
    SPI_Handle.WthenRs = rt_spi_send_then_recv;

    return RT_EOK;
}
/* 导出到自动初始化 */
INIT_PREV_EXPORT(spi_device_init);

